<template>
	<view class="container">
		<view class="search-box">
			<view class="input-box">
				<input type="text" :placeholder="defaultKeyword" v-model="keyword" @confirm="doSearch(false)" confirm-type="search">
			</view>
			<view class="search-btn" @click="doSearch(false)">搜索</view>
		</view>
		<view class="search-keyword" @touchstart="blur">
			<scroll-view class="keyword-box" scroll-y>
				<view class="keyword-block" v-if="oldKeywordList.length>0">
					<view class="keyword-list-header">
						<view>历史搜索</view>
						<view>
							<image @click="oldDelete" src="/static/delete.png"></image>
						</view>
					</view>
					<view class="keyword">
						<view v-for="(keyword,index) in oldKeywordList" @click="doSearch(keyword)" :key="index">
							{{keyword}}
						</view>
					</view>
				</view>
				<view class="keyword-block">
					<view class="keyword-list-header">
						<view>热门搜索</view>
						<view>
							<image @click="hotToggle" src="/static/refresh.png"></image>
						</view>
					</view>
					<view class="keyword">
						<view v-for="(keyword,index) in hotKeywordList" @click="doSearch(keyword)" :key="index">
							{{keyword}}
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				defaultKeyword: "",
				keyword: "",
				oldKeywordList: [],
				hotKeywordList: [],
				keywordList: [],
				forbid: ''
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			init() {
				this.loadDefaultKeyword()
				this.loadOldKeyword()
				this.loadHotKeyword()
			},
			blur() {
				uni.hideKeyboard()
			},
			//加载默认搜索关键字
			loadDefaultKeyword() {
				//定义默认搜索关键字，可以自己实现ajax请求数据再赋值,用户未输入时，以水印方式显示在输入框，直接不输入内容搜索会搜索默认关键字
				this.defaultKeyword = "默认关键字"
			},
			//加载历史搜索,自动读取本地Storage
			loadOldKeyword() {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						var OldKeys = JSON.parse(res.data)
						this.oldKeywordList = OldKeys
					}
				})
			},
			//加载热门搜索
			loadHotKeyword() {
				//定义热门搜索关键字，可以自己实现ajax请求数据再赋值
				this.hotKeywordList = []
			},
			//清除历史搜索
			oldDelete() {
				uni.showModal({
					title: '提示',
					content: '确定清除历史搜索记录?',
					success: (res) => {
						if (res.confirm) {
							console.log('用户点击确定')
							this.oldKeywordList = []
							uni.removeStorage({
								key: 'OldKeys'
							})
						} else if (res.cancel) {
							console.log('用户点击取消')
						}
					}
				})
			},
			//执行搜索
			doSearch(key) {
				key = key ? key : this.keyword ? this.keyword : this.defaultKeyword
				this.keyword = key
				this.saveKeyword(key) //保存为历史
				uni.navigateTo({
					url: `/pages/product/list?keywords=${key}`
				})
			},
			//保存关键字到历史记录
			saveKeyword(keyword) {
				uni.getStorage({
					key: 'OldKeys',
					success: (res) => {
						console.log(res.data)
						var OldKeys = JSON.parse(res.data)
						var findIndex = OldKeys.indexOf(keyword)
						if (findIndex == -1) {
							OldKeys.unshift(keyword)
						} else {
							OldKeys.splice(findIndex, 1)
							OldKeys.unshift(keyword)
						}
						//最多10个纪录
						OldKeys.length > 10 && OldKeys.pop()
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						})
						this.oldKeywordList = OldKeys //更新历史搜索
					},
					fail: (e) => {
						var OldKeys = [keyword]
						uni.setStorage({
							key: 'OldKeys',
							data: JSON.stringify(OldKeys)
						})
						this.oldKeywordList = OldKeys //更新历史搜索
					}
				})
			}
		}
	}
</script>
<style lang="scss">
	.search-box {
		width: 100%;
		background-color: rgb(242, 242, 242);
		padding: 20upx 30upx;
		display: flex;
		justify-content: space-between;
	}

	.search-box .mSearch-input-box {
		width: 100%;
	}

	.search-box .input-box {
		width: 85%;
		flex-shrink: 1;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.search-box .search-btn {
		width: 15%;
		margin: 0 0 0 10upx;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-shrink: 0;
		font-size: $base-font-base;
		color: #fff;
		background: linear-gradient(to top, #207fff, #0251f4);
		border-radius: 60upx;
	}

	.search-box .input-box>input {
		width: 100%;
		height: 60upx;
		font-size: $base-font-base;
		color: $base-color-light;
		border: 0;
		border-radius: 60upx;
		appearance: none;
		padding: 0 20upx 0 60upx;
		margin: 0;
		background-color: #ffffff;
		background-image: url('');
		background-position: left 15upx center;
		background-repeat: no-repeat;
		background-size: 40upx 40upx;
	}

	.placeholder-class {
		color: $base-color-light;
	}

	.search-keyword {
		width: 100%;
		background-color: rgb(242, 242, 242);
	}

	.keyword-list-box {
		height: calc(100vh - 110upx);
		padding-top: 10upx;
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-entry-tap {
		background-color: #eee;
	}

	.keyword-entry {
		width: 94%;
		height: 80upx;
		margin: 0 20upx;
		font-size: 30upx;
		color: $base-color-page;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: solid 1upx #e7e7e7;
	}

	.keyword-entry image {
		width: 60upx;
		height: 60upx;
	}

	.keyword-entry .keyword-text,
	.keyword-entry .keyword-img {
		height: 80upx;
		display: flex;
		align-items: center;
	}

	.keyword-entry .keyword-text {
		width: 90%;
	}

	.keyword-entry .keyword-img {
		width: 10%;
		justify-content: center;
	}

	.keyword-box {
		height: calc(100vh - 110upx);
		border-radius: 20upx 20upx 0 0;
		background-color: #fff;
	}

	.keyword-box .keyword-block {
		padding: 10upx 0;
	}

	.keyword-box .keyword-block .keyword-list-header {
		position: relative;
		padding: 20upx 30upx 20upx 50upx;
		font-size: $base-font-base;
		color: $base-color-dark;
		line-height: 40upx;
		display: flex;
		justify-content: space-between;

		&:before {
			content: '';
			position: absolute;
			left: 20upx;
			top: 50%;
			transform: translateY(-50%);
			height: 36upx;
			width: 8upx;
			background-color: $base-color-page;
			border-radius: 0 4px 4px 0;
			opacity: .8;
		}
	}

	.keyword-box .keyword-block .keyword-list-header image {
		width: 40upx;
		height: 40upx;
		vertical-align: top;
	}

	.keyword-box .keyword-block .keyword {
		padding: 0 30upx;
		display: flex;
		flex-flow: wrap;
		justify-content: flex-start;
	}

	.keyword-box .keyword-block .hide-hot-tis {
		display: flex;
		justify-content: center;
		font-size: $base-font-base;
		color: $base-color-base;
	}

	.keyword-box .keyword-block .keyword>view {
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 60upx;
		padding: 0 20upx;
		margin: 10upx 20upx 10upx 0;
		height: 50upx;
		font-size: $base-font-sm;
		background-color: rgb(242, 242, 242);
		color: $base-color-base;
	}
</style>
